home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / archie-1.4.1 / archie.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-18  |  7.2 KB  |  324 lines

  1. /*
  2.  * Copyright (c) 1991 by the University of Washington
  3.  *
  4.  * For copying and distribution information, please see the file
  5.  * <copyright.h>.
  6.  */
  7.  
  8. /*
  9.  * Archie client using the Prospero protocol.
  10.  *
  11.  * Suggestions and improvements to Brendan Kehoe (brendan@cygnus.com).
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <getopt.h>
  16.  
  17. #if defined(OS2)
  18. # include <pctcp.h>
  19. #endif
  20.  
  21. #ifdef MSDOS
  22. # include <string.h>
  23. # include <stdlib.h>
  24. # ifdef CUTCP
  25. #  include <msdos/cutcp.h>
  26. #  include <msdos/hostform.h>
  27. /* The default stack size for a BC program is 4k; jack it up to 16 and add the
  28.    Check for Stack Overflow option to the compiler.  */
  29. extern unsigned _stklen = 16 * 1024;
  30. # endif
  31. #endif
  32.  
  33. #include <pfs.h>
  34. #include <rdgram.h>
  35. #include <archie.h>
  36. #include <pmachine.h>
  37.  
  38. /* Whether we should produce single-line listings suitable for frobbing by
  39.    other programs, or produce nice clean human output (default).  */
  40. int listflag = 0;
  41.  
  42. /* How to sort the data; 1 means by date, 0 is by inverse hostname.  */
  43. int sortflag = 0;
  44.  
  45. /* Used by CUTCP to see if they specified the host with `-h' or if
  46.    the config.tel file should be consulted.  */ 
  47. int hostset = 0;
  48.  
  49. /* When doing searches, should we make some comments to pacify the user?  */
  50. int verbose = 0;
  51.  
  52. /* Maximum number of hits for this query; pushing this high is very
  53.    anti-social.  */
  54. int max_hits = MAX_HITS;
  55.  
  56. /* The offset for the Prospero query.  */
  57. int offset = 0;
  58.  
  59. /* Display the Alex filename?  */
  60. int alex = 0;
  61.  
  62. /* The default host to query for searches.  */ 
  63. char *host = ARCHIE_HOST;
  64.  
  65. FILE *archie_out;
  66.  
  67. /* The name this program was run with.  */
  68. char *program_name;
  69.  
  70. extern int pfs_debug;
  71. extern int rdgram_priority;
  72.  
  73. void usage ();
  74. extern char *getenv ();
  75.  
  76. void
  77. main (argc, argv)
  78.      int argc;
  79.      char **argv;
  80. {
  81.   Query query = EXACT;
  82.   int optc, tmp;
  83.   /* If true, display the release.  */
  84.   int exitflag = 0;
  85.   /* The file to print the results to.  Defaults to stdout.  */
  86.   char *outfile = (char *)NULL;
  87.   char *p;
  88.   static char *archies[] = { ARCHIES };
  89.  
  90.   program_name = argv[0];
  91.  
  92.   /* Default debugging level.  */
  93.   pfs_debug = 0;
  94.  
  95. #ifdef    CUTCP
  96.   if (getenv ("CONFIGTEL"))
  97.     if (Shostfile (getenv ("CONFIGTEL")) < 0)
  98.       {
  99.     fprintf (stderr, "Error, couldn't open configtel file %s\n",
  100.          getenv ("CONFIGTEL"));
  101.     exit (1);
  102.       }
  103. #endif
  104.  
  105.   if ((p = getenv ("ARCHIE_HOST")) != (char *) NULL)
  106.     host = p;
  107.  
  108. #ifdef CUTCP
  109.   while ((optc = getopt (argc, argv, "D:LHN::O:ceh:alm:o:rstvV")) != EOF)
  110. #else
  111.   while ((optc = getopt (argc, argv, "D:LN::O:ceh:alm:o:rstvV")) != EOF)
  112. #endif
  113.     {
  114.       switch (optc)
  115.     {
  116.     case 'D':
  117.       pfs_debug = atoi (optarg);
  118.       break;
  119.  
  120.     case 'L':
  121.       printf ("Known archie servers:\n");
  122.       for (tmp = 0; tmp < NARCHIES; tmp++)
  123.         printf ("\t%s\n", archies[tmp]);
  124.       printf (" * %s is the default Archie server.\n", ARCHIE_HOST);
  125.       printf (" * For the most up-to-date list, write to an Archie server and give it\n   the command `servers'.\n");
  126.       exitflag = 1;
  127.       break;
  128.  
  129. #ifdef CUTCP
  130.     case 'H':
  131.       if (Shostfile (optarg) < 0)
  132.         {
  133.           fprintf (stderr,
  134.                "%s: couldn't open configtel file %s\n",
  135.                program_name, optarg);
  136.           exit (1);
  137.         }
  138.       break;
  139. #endif
  140.  
  141.     case 'N':
  142.       if (optarg)
  143.         {
  144.           rdgram_priority = atoi (optarg);
  145.           if (rdgram_priority > RDGRAM_MAX_SPRI)
  146.         rdgram_priority = RDGRAM_MAX_PRI;
  147.           else if (rdgram_priority < RDGRAM_MIN_PRI)
  148.         rdgram_priority = RDGRAM_MIN_PRI;
  149.         }
  150.       else
  151.         rdgram_priority = RDGRAM_MAX_PRI;
  152.       break;
  153.  
  154.     case 'c': /* Substring (case-sensitive).  */
  155.       query = SUBSTRING_CASE;
  156.       break;
  157.  
  158.     case 'e': /* Exact match.  */
  159.       query = EXACT;
  160.       break;
  161.  
  162.     case 'h': /* Archie host.  */
  163.       host = optarg;
  164. #ifdef CUTCP
  165.       hostset = 1;
  166. #endif
  167.       break;
  168.  
  169.     case 'a': /* List matches as Alex filenames.  */
  170.       alex = 1;
  171.       break;
  172.  
  173.     case 'l': /* List one match per line.  */
  174.       listflag = 1;
  175.       break;
  176.  
  177.     case 'm': /* Maximum number of hits for the query.  */
  178.       max_hits = atoi (optarg);
  179.       if (max_hits < 1)
  180.         {
  181.           fprintf (stderr,
  182.                "%s: option `-m' requires a max hits value >= 1\n",
  183.                program_name);
  184.           exit (ERROR_EXIT);
  185.         }
  186.       break;
  187.  
  188.     case 'o': /* output file */
  189.       if (outfile)
  190.         {
  191.           fprintf (stderr, "%s: multiple output files specified\n",
  192.                program_name);
  193.           exit (ERROR_EXIT);
  194.         }
  195.       outfile = optarg;
  196.       break;
  197.  
  198.     case 'O': /* Specify the offset.  */
  199.       offset = atoi (optarg);
  200.       break;
  201.  
  202.     case 'r': /* Regexp search.  */
  203.       query = REGEXP;
  204.       break;
  205.  
  206.     case 's': /* Substring (case insensitive).  */
  207.       query = SUBSTRING;
  208.       break;
  209.  
  210.     case 't': /* Sort inverted by date.  */
  211.       sortflag = 1;
  212.       break;
  213.  
  214.     case 'v': /* Display version.  */
  215.       fprintf (stderr,
  216.            "Client version %s based upon Prospero version %s%s\n",
  217.            CLIENT_VERSION,
  218. #ifdef DEBUG
  219.            PFS_RELEASE, " with debugging.");
  220. #else
  221.            PFS_RELEASE, ".");
  222. #endif
  223.       exitflag = 1;
  224.       break;
  225.  
  226.     case 'V': /* Verbose when search is happening.  */
  227.       verbose = 1;
  228.       break;
  229.  
  230.     default:
  231.       usage ();
  232.     }
  233.     }
  234.  
  235.   if (exitflag)
  236.     exit (0);
  237.   else if (optind == argc)
  238.     usage ();
  239.   else if (alex && listflag)
  240.     {
  241.       fprintf (stderr, "%s: only one of `-a' or `-l' may be used\n",
  242.            program_name);
  243.       exit (ERROR_EXIT);
  244.     }
  245.  
  246.   if (outfile)
  247.     {
  248.       archie_out = fopen (outfile, "w+");
  249.       if (archie_out == (FILE *) NULL)
  250.     {
  251.       fprintf (stderr, "%s: cannot open %s\n", program_name, outfile);
  252.       exit (ERROR_EXIT);
  253.     }
  254.     }
  255.   else
  256.     archie_out = stdout;
  257.  
  258. #ifdef    CUTCP
  259.   if (tmp = Snetinit ())
  260.     {
  261.       fprintf (stderr, " %d from SNetinit (bad or missing config.tel ?)\n",
  262.            tmp);
  263.  
  264.       /* If there was a rarp lookup failure, shut the network card down.  */
  265.       if (tmp == -2)
  266.     netshut ();
  267.       exit (ERROR_EXIT);
  268.     }
  269.  
  270.   if (!hostset)
  271.     {
  272.       /* Look in config.tel if they didn't give us a host with `-h'.  The
  273.          entries appear as "name=archie".  */
  274.       struct machinfo *mp = Shostlook ("archie");
  275.  
  276.       if (mp)
  277.     host = mp->hname ? mp->hname : mp->sname;
  278.     }
  279. #endif
  280.  
  281.   for (; optind < argc; ++optind)
  282.     procquery (host, argv[optind], max_hits, offset, query);
  283.  
  284. #ifdef CUTCP
  285.   netshut ();
  286. #endif
  287.  
  288.   if (outfile)
  289.     fclose (archie_out);
  290.  
  291.   exit (0);
  292. }
  293.  
  294. #ifdef CUTCP
  295. # define HFLAG    "] [H config.tel]"
  296. #else
  297. # define HFLAG    "]"
  298. #endif
  299.  
  300. void
  301. usage ()
  302. {
  303.   fprintf (stderr, "\
  304. Usage: %s [-acelorstvLV] [-m hits%s [-N level] string\n", program_name, HFLAG);
  305.   fprintf (stderr, "          -a : list matches as Alex filenames\n");
  306.   fprintf (stderr, "          -c : case sensitive substring search\n");
  307.   fprintf (stderr, "          -e : exact string match (default)\n");
  308.   fprintf (stderr, "          -r : regular expression search\n");
  309.   fprintf (stderr, "          -s : case insensitive substring search\n");
  310.   fprintf (stderr, "          -l : list one match per line\n");
  311.   fprintf (stderr, "          -t : sort inverted by date\n");
  312.   fprintf (stderr, "     -m hits : specifies maximum number of hits to return (default %d)\n", max_hits);
  313.   fprintf (stderr, " -o filename : specifies file to store results in\n");
  314.   fprintf (stderr, "     -h host : specifies server host\n");
  315.   fprintf (stderr, "          -L : list known servers and current default\n");
  316.   fprintf (stderr, "    -N level : specifies query niceness level (0-35765)\n");
  317. #ifdef CUTCP
  318.   fprintf (stderr, "-H config.tel: specify location of config.tel file\n");
  319. #endif
  320.  
  321.   exit (ERROR_EXIT);
  322. }
  323.  
  324.